﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SFXManager : Singleton<SFXManager>
{
    public Dictionary<string,AudioSource> audioSourceDic = new Dictionary<string, AudioSource>();
    public Dictionary <string, AudioClip> audioClipDic= new Dictionary<string, AudioClip>();
    public override void Init()
    {
        base.Init();
        // if (!audioSource)
        // {
        //     audioSource = this.gameObject.AddComponent<AudioSource>();
        // }
    }
    public void PlaySFX(string clipName,float volume=1f)
    {
        if(!audioSourceDic.ContainsKey(clipName))
        {
             GameObject obj = new GameObject();
             obj.transform.SetParent(this.transform);
             obj.name = clipName+"AudioSource";
             audioSourceDic.Add(clipName,obj.AddComponent<AudioSource>());
             DontDestroyOnLoad(obj);
        }
        if (audioSourceDic[clipName] != null)
        {   
            audioSourceDic[clipName].volume = volume;
            if(audioClipDic.ContainsKey(clipName))
            {   
                AudioManager.Instance.PlaySFX(audioSourceDic[clipName], audioClipDic[clipName]);
            }
            {
                 audioClipDic[clipName] = Resources.Load<AudioClip>("AudioClip/"+clipName);
                 AudioManager.Instance.PlaySFX(audioSourceDic[clipName], audioClipDic[clipName]);
            }
        }
    }

    internal void PlaySFX(object collectSFX)
    {
        throw new NotImplementedException();
    }
}
